package logic

import (
	"2211a_mayi/service/mayi"
	"context"

	"2211a_mayi/api/internal/svc"
	"2211a_mayi/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderRequest) (resp *types.Response, err error) {
	userId := l.ctx.Value("userId").(uint)

	res, _err := l.svcCtx.Service.CreateOrder(l.ctx, &mayi.CreateOrderRequest{
		GoodsId: int64(req.GoodsId),
		UserId:  int64(userId),
		Num:     int64(req.GoodsNum),
	})

	if _err != nil {
		return &types.Response{
			Code: 500,
			Msg:  "创建订单失败",
		}, err
	}

	return &types.Response{
		Code: 200,
		Msg:  "创建订单成功",
		Data: map[string]interface{}{
			"pay_url": res.PayUrl,
		},
	}, nil
}
